const TurnHole = function (params) {
    var handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
    // var entity = viewer.entities.add({
    // 	id: "myEntity",
    // 	position: Cesium.Cartesian3.fromDegrees(114.33, 30.35, 100),
    // 	model: {
    // 		uri: 'http://106.12.254.129:39895/data/models3D/d1.gltf',
    // 		minimumPixelSize: 128, //模型最小显示尺寸
    // 		maximumScale: 20000,
    // 		color: Cesium.Color.RED,
    // 		colorBlendMode: Cesium.ColorBlendMode.REPLACE //颜色如何与模型混合
    // 	}
    // });

    // // var objectsToExclude = [entity];
    // viewer.zoomTo(entity);


    // var entity1 = viewer.entities.add({
    // 	id: "myEntity1",
    // 	position: Cesium.Cartesian3.fromDegrees(114.33, 30.35, 150),
    // 	model: {
    // 		uri: 'http://106.12.254.129:39895/data/models3D/d1.gltf',
    // 		minimumPixelSize: 128, //模型最小显示尺寸
    // 		maximumScale: 20000,
    // 		color: Cesium.Color.YELLOW,
    // 		colorBlendMode: Cesium.ColorBlendMode.REPLACE //颜色如何与模型混合
    // 	}
    // });
    // var entity2 = viewer.entities.add({
    // 	id: "myEntity2",
    // 	position: Cesium.Cartesian3.fromDegrees(114.33, 30.35, 200),
    // 	model: {
    // 		uri: 'http://106.12.254.129:39895/data/models3D/d1.gltf',
    // 		minimumPixelSize: 128, //模型最小显示尺寸
    // 		maximumScale: 20000,
    // 		color: Cesium.Color.BLUE,
    // 		colorBlendMode: Cesium.ColorBlendMode.REPLACE //颜色如何与模型混合
    // 	}
    // });
    // viewer.zoomTo(entity1);

    handler.setInputAction(function (clickEvent) {
        var a = viewer.scene._imageryLayerCollection;
        //获取地形高度
        //获取地形高度 获取加载地形后对应的经纬度和高程
        var ray1 = viewer.camera.getPickRay(clickEvent.position);
        var cartesian = viewer.scene.globe.pick(ray1, viewer.scene);
        var pick = viewer.scene.pickPosition(clickEvent.position);
        // 从场景的深度缓冲区中拾取相应的位置，返回笛卡尔坐标
        var pickModel = viewer.scene.pick(clickEvent.position);

        var drillPick = viewer.scene.drillPick(clickEvent.position, 8, 4, 4);
        var ens = [];

        var entities = viewer.entities.values;
        var entityIds = ["myEntity2", "myEntity1", "myEntity"];
        var coordinations = [];
        var lat, lng;
        for (var i = 0; i < entities.length; i++) {
            if (entityIds.indexOf(entities[i].id) <= -1) {
                ens.push(entities[i]);
            };
        }
        for (var j = 0; j < entityIds.length; j++) {
            if (pickModel && pick && pickModel.id) {
                let longMin = Number.MIN_VALUE;
                let longMax = 0
                let widthMin = Number.MIN_VALUE;
                let widthMax = 0
                let heightMin = Number.MIN_VALUE;
                let heightMax = 0

                pickModel.primitive.gltf.accessors.forEach(item => {
                    if (item.name.indexOf('positions') !== -1) {
                        if (item.max[0] > longMax) {
                            longMax = item.max[0]
                        }
                        if (item.min[0] < longMin) {
                            longMin = item.min[0]
                        }
                        if (item.max[2] > widthMax) {
                            widthMax = item.max[2]
                        }
                        if (item.min[2] < widthMin) {
                            widthMin = item.min[2]
                        }
                        if (item.max[1] > heightMax) {
                            heightMax = item.max[1]
                        }
                        if (item.min[1] < heightMin) {
                            heightMin = item.min[1]
                        }
                    }
                })
                var long = Number((longMax - longMin).toFixed(3))
                var width = Number((widthMax - widthMin).toFixed(3))
                var height = Number((heightMax - heightMin).toFixed(3))

                //var height = Cesium.Cartographic.fromCartesian(pick).height;
                // 当前视图中渲染的Globe瓷砖和3D Tile采样高度

                var height = viewer.scene.sampleHeight(Cesium.Cartographic.fromCartesian(pick), ens);
                
                lat = Cesium.Math.toDegrees(Cesium.Cartographic.fromCartesian(pick).latitude);
                lng = Cesium.Math.toDegrees(Cesium.Cartographic.fromCartesian(pick).longitude);
                cartesian = Cesium.Cartesian3.fromDegrees(lng, lat, height);
                ens.push(viewer.entities.getById(entityIds[j]));
                coordinations.push({
                    "lng": lng,
                    "lat": lat,
                    "height": height
                });
                // viewer.entities.getById(entityIds[j]).show = false;

            }
        }
        if (coordinations.length > 0) {
            coordinations.push({
                "lng": lng,
                "lat": lat,
                "height": 0.0
            });
            //开始绘制钻孔
            for (var k = 1; k < coordinations.length; k++) {
                var zkCoorDinations = [];
                zkCoorDinations.push(coordinations[k].lng);
                zkCoorDinations.push(coordinations[k].lat);
                zkCoorDinations.push(coordinations[k].height);
                zkCoorDinations.push(coordinations[k - 1].lng);
                zkCoorDinations.push(coordinations[k - 1].lat);
                zkCoorDinations.push(coordinations[k - 1].height);
                if (k === 1) {
                    // viewer.entities.add({
                    // 	polylineVolume: {
                    // 		positions: Cesium.Cartesian3.fromDegreesArrayHeights(zkCoorDinations),
                    // 		shape: computeCircle(5),
                    // 		cornerType: Cesium.CornerType.MITERED,
                    // 		// material: new Cesium.PolylineVolumeTrialMaterialProperty(Cesium.Color.BLUE, 1000, 5),
                    // 		material: Cesium.Color.BLUE.withAlpha(1.0)
                    // 	}
                    // });
                    var height = (coordinations[k].height + coordinations[k - 1].height) / 2.0
                    var position = Cesium.Cartesian3.fromDegrees(
                        coordinations[k].lng,
                        coordinations[k].lat,
                        height
                    );
                    viewer.entities.add({
                        position: position,
                        cylinder: {
                            length: coordinations[k - 1].height - coordinations[k].height,
                            topRadius: 5,
                            bottomRadius: 5,
                            material: Cesium.Color.BLUE
                        }
                    });
                }
                if (k === 2) {
                    // viewer.entities.add({
                    // 	polylineVolume: {
                    // 		positions: Cesium.Cartesian3.fromDegreesArrayHeights(zkCoorDinations),
                    // 		shape: computeCircle(5),
                    // 		cornerType: Cesium.CornerType.MITERED,
                    // 		// material: new Cesium.PolylineVolumeTrialMaterialProperty(Cesium.Color.BLUE, 1000, 5),
                    // 		material: Cesium.Color.YELLOW.withAlpha(1.0)
                    // 	}
                    // });
                    var height = (coordinations[k].height + coordinations[k - 1].height) / 2.0
                    var position = Cesium.Cartesian3.fromDegrees(
                        coordinations[k].lng,
                        coordinations[k].lat,
                        height
                    );
                    viewer.entities.add({
                        position: position,
                        cylinder: {
                            length: coordinations[k - 1].height - coordinations[k].height,
                            topRadius: 5,
                            bottomRadius: 5,
                            material: Cesium.Color.YELLOW
                        }
                    });
                }
                if (k === 3) {
                    // viewer.entities.add({
                    // 	polylineVolume: {
                    // 		positions: Cesium.Cartesian3.fromDegreesArrayHeights(zkCoorDinations),
                    // 		shape: computeCircle(5),
                    // 		cornerType: Cesium.CornerType.MITERED,
                    // 		// material: new Cesium.PolylineVolumeTrialMaterialProperty(Cesium.Color.BLUE, 1000, 5),
                    // 		material: Cesium.Color.RED.withAlpha(1.0)
                    // 	}
                    // });

                    var height = (coordinations[k].height + coordinations[k - 1].height) / 2.0
                    var position = Cesium.Cartesian3.fromDegrees(
                        coordinations[k].lng,
                        coordinations[k].lat,
                        height
                    );
                    viewer.entities.add({
                        position: position,
                        cylinder: {
                            length: coordinations[k - 1].height - coordinations[k].height,
                            topRadius: 5,
                            bottomRadius: 5,
                            material: Cesium.Color.RED
                        }
                    });
                }
            }



        }


        //重新根据模型获取模型的表面坐标
        var ens = [];
        var entities = viewer.entities.values;
        var entityIds = ["myEntity2", "myEntity1", "myEntity"];
        var polyGonsArr = [];
        var interpolationNum = 100; //插值点数
        var startPoint = {
            "x": 1025,
            "y": 522
        };
        var endPoint = {
            "x": 1302,
            "y": 735
        }

        for (var i = 0; i < entities.length; i++) {
            if (entityIds.indexOf(entities[i].id) <= -1) {
                ens.push(entities[i]);
            };
        }

        var index = 0;
        var xdiffer = (endPoint.x - startPoint.x) / interpolationNum;
        var ydiffer = (endPoint.y - startPoint.y) / interpolationNum;
        var pickStart = viewer.scene.pickPosition(new Cesium.Cartesian2(startPoint.x, startPoint.y));
        var positions = [];
        positions.push(Cesium.Cartographic.fromCartesian(pickStart));
        for (var i = 1; i <= interpolationNum; i++) {
            positions.push(Cesium.Cartographic.fromCartesian(viewer.scene.pickPosition(new Cesium.Cartesian2(
                startPoint
                    .x + xdiffer * i, startPoint.y + ydiffer * i))));
        }
        var pickEnd = viewer.scene.pickPosition(new Cesium.Cartesian2(endPoint.x, endPoint.y));
        positions.push(Cesium.Cartographic.fromCartesian(pickEnd));
        ///   
        var promiseArr = [];
        var a = [];
        for (var i = 0; i < entities.length; i++) {
            if (entityIds.indexOf(entities[i].id) <= -1) {
                a.push(entities[i]);
            };
        }
        var promiseExtEntArr = [];
        promiseExtEntArr.push(Object.assign([], ens));
        // promiseArr.push(viewer.scene.sampleHeightMostDetailed(positions, Object.assign([],ens)));
        for (var j = 0; j < entityIds.length; j++) {
            if (j < entityIds.length - 1) {
                ens.push(viewer.entities.getById(entityIds[j]));
                promiseExtEntArr.push(Object.assign([], ens));
            }
        }

        //promiseArr.push(viewer.scene.sampleHeightMostDetailed(positions, b)
        // entityIds.forEach((item,index) => {
        // 	if(index<entityIds.length-1){
        // 		ens.push(viewer.entities.getById(item));
        // 		var arr=[];
        // 		ens.forEach((item,index) => {
        // 			arr.push(item);
        // 		});
        // 		promiseArr.push(viewer.scene.sampleHeightMostDetailed(positions, arr));
        // 	}
        // });
        var polyGonPosArr = [];
        var maxHeightArr = [];
        var minHeightArr = [];
        var colorArr = [Cesium.Color.BLUE, Cesium.Color.YELLOW, Cesium.Color.RED];

        function loopArray(x) {
            viewer.scene.sampleHeightMostDetailed(positions, promiseExtEntArr[x]).then(function (updatedPosition) {
                var polyGonPos = [];
                var maxHeight = [];
                var minHeight = [];
                for (var i = 0; i < updatedPosition.length; i++) {
                    var lat = Cesium.Math.toDegrees(updatedPosition[i].latitude);
                    var lng = Cesium.Math.toDegrees(updatedPosition[i].longitude);
                    var height = updatedPosition[i].height;
                    polyGonPos.push(lng);
                    polyGonPos.push(lat);
                    polyGonPos.push(height);
                    maxHeight.push(height);
                }
                polyGonPosArr.push(polyGonPos);
                maxHeightArr.push(maxHeight);
                if (x > 0) {
                    //组织每层底面高程
                    var minHeight = [];
                    for (var i = 0; i < updatedPosition.length; i++) {
                        minHeight.push(height);
                    }
                    minHeightArr.push(minHeight);
                }
                x++;
                if (x < promiseExtEntArr.length) {

                    loopArray(x);
                } else {
                    //填充最后一个地层的底面高程
                    var minHeight = [];
                    for (var i = 0; i < updatedPosition.length; i++) {
                        minHeight.push(0);
                    }
                    minHeightArr.push(minHeight);
                    //开始绘制面
                    for (var i = 0; i < polyGonPosArr.length; i++) {
                        var polygon = viewer.entities.add({
                            wall: {
                                positions: Cesium.Cartesian3.fromDegreesArrayHeights(polyGonPosArr[i]),
                                maximumHeights: maxHeightArr[i],
                                minimumHeights: minHeightArr[i],
                                outline: false,
                                outlineColor: colorArr[i],
                                outlineWidth: 4,
                                material: colorArr[i],
                                // Cesium.Color.fromRandom({
                                // 	alpha: 1.0
                                // }),
                            },
                        });
                    }
                    //地质体隐藏
                    entityIds.forEach(item => {
                        viewer.entities.getById(item).show = false;
                    })

                    var clippingPlanes = new Cesium.ClippingPlaneCollection({
                        planes: [
                            // new Cesium.ClippingPlane(new Cesium.Cartesian3(0.0, 0.5, 0.0), 0.0),
                            new Cesium.ClippingPlane(new Cesium.Cartesian3(0.0, -0.5, 0.0), 1.0)
                        ],
                        unionClippingRegions: true,
                        edgeColor: Cesium.Color.BLUE,
                        edgeWidth: 3.0
                    });
                    // Create an entity and attach the ClippingPlaneCollection to the model.
                    var entity = viewer.entities.add({
                        position: Cesium.Cartesian3.fromDegrees(106.0744619, 35.0503706, 50),
                        model: {
                            uri: 'http://106.12.254.129:39895/data/models3D/d1.gltf',
                            minimumPixelSize: 128,
                            maximumScale: 20000,
                            //clippingPlanes : clippingPlanes
                        }
                    });
                    viewer.zoomTo(entity);

                }


            });
        }
        loopArray(0);
        // function loopArray(){
        // 	viewer.scene.sampleHeightMostDetailed(positions, item).then()
        // }
        // Promise.all[promiseExtEntArr.map(item => viewer.scene.sampleHeightMostDetailed(positions, item))].then(dataList => {
        // })

        // promise.then(function(updatedPosition) {
        // 	// positions[0].height and positions[1].height have been updated.
        // 	// updatedPositions is just a reference to positions.
        // 	var polyGonArr = [];
        // 	var maxHeight = [];
        // 	var minHeight = [];
        // 	for (var i = 0; i < updatedPosition.length; i++) {
        // 		var lat = Cesium.Math.toDegrees(updatedPosition[i].latitude);
        // 		var lng = Cesium.Math.toDegrees(updatedPosition[i].longitude);
        // 		var height = updatedPosition[i].height;
        // 		polyGonArr.push(lng);
        // 		polyGonArr.push(lat);
        // 		polyGonArr.push(height);
        // 		maxHeight.push(height);

        // 		minHeight.push(100);
        // 	}
        // 	// for(var j=updatedPosition.length-1;j>=0;j--){
        // 	// 	var lat = Cesium.Math.toDegrees(updatedPosition[j].latitude);
        // 	// 	var lng = Cesium.Math.toDegrees(updatedPosition[j].longitude);
        // 	// 	var height = 0;
        // 	// 	polyGonArr.push(lng);
        // 	// 	polyGonArr.push(lat);
        // 	// 	polyGonArr.push(height);
        // 	// }
        // 	// var polygon = viewer.entities.add({
        // 	// 	polygon: {
        // 	// 		hierarchy: new Cesium.PolygonHierarchy(
        // 	// 			Cesium.Cartesian3.fromDegreesArray(polyGonArr)
        // 	// 		),
        // 	// 		material: Cesium.Color.RED,
        // 	// 		// classificationType: Cesium.ClassificationType.BOTH,
        // 	// 	},
        // 	// });

        // 	polyGonsArr.push(polyGonArr);
        // 	maxHeightArr.push(maxHeight);


        // 	index++;
        // 	if(index<entityIds.length){

        // 	}

        // 	// var polygon = viewer.entities.add({
        // 	// 	wall: {
        // 	// 		positions: Cesium.Cartesian3.fromDegreesArrayHeights(polyGonArr),
        // 	// 		maximumHeights: maxHeight,
        // 	// 		minimumHeights: minHeight,
        // 	// 		outline: false,
        // 	// 		outlineColor: Cesium.Color.LIGHTGRAY,
        // 	// 		outlineWidth: 4,
        // 	// 		material: Cesium.Color.fromRandom({
        // 	// 			alpha: 1.0
        // 	// 		}),
        // 	// 	},
        // 	// });
        // 	// // If the mouse is over the billboard, change its scale and color
        // 	// var handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
        // 	// handler.setInputAction(function(movement) {
        // 	// 	var pickedObject = viewer.scene.pick(movement.position);
        // 	// 	if (Cesium.defined(pickedObject) && pickedObject.id === polygon) {
        // 	// 		alert("模型被选中了");
        // 	// 	} else {
        // 	// 		alert("选中取消");
        // 	// 	}
        // 	// }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
        // });








        // var polyGonArr = [];
        // var maxHeight = [];
        // var minHeight = [];
        // var xdiffer = (endPoint.x - startPoint.x) / interpolationNum;
        // var ydiffer = (endPoint.y - startPoint.y) / interpolationNum;
        // var pickStart = viewer.scene.pickPosition(new Cesium.Cartesian2(startPoint.x, startPoint.y));
        // var positions = [];
        // positions.push(Cesium.Cartographic.fromCartesian(pickStart));
        // for (var i = 1; i <= interpolationNum; i++) {
        // 	positions.push(Cesium.Cartographic.fromCartesian(viewer.scene.pickPosition(new Cesium.Cartesian2(startPoint
        // 		.x + xdiffer * i, startPoint.y + ydiffer * i))));
        // }
        // var pickEnd = viewer.scene.pickPosition(new Cesium.Cartesian2(endPoint.x, endPoint.y));
        // positions.push(Cesium.Cartographic.fromCartesian(pickEnd));
        // var promise = viewer.scene.sampleHeightMostDetailed(positions);
        // promise.then(function(updatedPosition) {
        // 	// positions[0].height and positions[1].height have been updated.
        // 	// updatedPositions is just a reference to positions.
        // 	for (var i = 0; i < updatedPosition.length; i++) {
        // 		var lat = Cesium.Math.toDegrees(updatedPosition[i].latitude);
        // 		var lng = Cesium.Math.toDegrees(updatedPosition[i].longitude);
        // 		var height = updatedPosition[i].height;
        // 		polyGonArr.push(lng);
        // 		polyGonArr.push(lat);
        // 		polyGonArr.push(height);
        // 		maxHeight.push(height);
        // 		minHeight.push(100);
        // 	}
        // 	// for(var j=updatedPosition.length-1;j>=0;j--){
        // 	// 	var lat = Cesium.Math.toDegrees(updatedPosition[j].latitude);
        // 	// 	var lng = Cesium.Math.toDegrees(updatedPosition[j].longitude);
        // 	// 	var height = 0;
        // 	// 	polyGonArr.push(lng);
        // 	// 	polyGonArr.push(lat);
        // 	// 	polyGonArr.push(height);
        // 	// }
        // 	// var polygon = viewer.entities.add({
        // 	// 	polygon: {
        // 	// 		hierarchy: new Cesium.PolygonHierarchy(
        // 	// 			Cesium.Cartesian3.fromDegreesArray(polyGonArr)
        // 	// 		),
        // 	// 		material: Cesium.Color.RED,
        // 	// 		// classificationType: Cesium.ClassificationType.BOTH,
        // 	// 	},
        // 	// });
        // 	var polygon = viewer.entities.add({
        // 		wall: {
        // 			positions: Cesium.Cartesian3.fromDegreesArrayHeights(polyGonArr),
        // 			maximumHeights: maxHeight,
        // 			minimumHeights: minHeight,
        // 			outline: false,
        // 			outlineColor: Cesium.Color.LIGHTGRAY,
        // 			outlineWidth: 4,
        // 			material: Cesium.Color.fromRandom({
        // 				alpha: 1.0
        // 			}),
        // 		},
        // 	});
        // 	// If the mouse is over the billboard, change its scale and color
        // 	var handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
        // 	handler.setInputAction(function(movement) {
        // 		var pickedObject = viewer.scene.pick(movement.position);
        // 		if (Cesium.defined(pickedObject) && pickedObject.id === polygon) {
        // 			alert("模型被选中了");
        // 		} else {
        // 			alert("选中取消");
        // 		}
        // 	}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
        // });

    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);

}

export default TurnHole